package com.bw.module_home.home.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bw.module_home.databinding.HomeMenuItemBinding
import com.bw.module_home.home.model.HomeModel

class HomeMenuAdapter: RecyclerView.Adapter<HomeViewHolder>() {
    private val list = mutableListOf<HomeModel>()

    var onItemClick:((HomeModel) -> Unit)? = null
    fun setData(data:List<HomeModel>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        Log.d("MYJ", "getItemCount: "+list.size)
        return list.size
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        val binding = HomeMenuItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return HomeViewHolder(binding)
    }
    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val binding= DataBindingUtil.getBinding<HomeMenuItemBinding>(holder.itemView) ?: return
        binding.homeMenuImage.setImageResource(list[position].img)
        binding.homeMenuTitle.text=list[position].title
        Log.d("MYJ", "onBindViewHolder: "+list[position].title)

        holder.itemView.setOnClickListener {
            onItemClick?.invoke(list[position])
        }
    }




}
class HomeViewHolder(val binding: HomeMenuItemBinding) : RecyclerView.ViewHolder(binding.root)
